import os
import logging
import game_mech

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db


def StartTTTGame(big_board):
  turn=1
  template_values = game_mech.ConverttoDict(big_board)
  return big_board,template_values, turn

def MoveTTT(col,row,big_board,turn):
  big_board,turn = game_mech.make_move(turn,col,row,big_board)
  logging.info(big_board)
  message,highlight = game_mech.checkifwon(big_board)
  if message != None:
    win = message
  else:
    win = " "
  return big_board, turn, win,highlight

def HighlightButton(highlight,template_values):
  button_color = [["","",""],["","",""],["","",""]]
  if highlight != "":
    for x in highlight:
       button_color[x[0]][x[1]] = "background-color:lightgreen"
  return button_color
    

class HomeHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), "home.html")
    self.response.out.write(template.render(path, template_values))

class CrazyGameHandler(webapp.RequestHandler):
  def get(self):
    turn = 1
    big_board = [[[0,0,0]for _ in range(3)]for _ in range(9)]
    template_values = game_mech.ConverttoDict(big_board)
    template_values["StringMatrix"] = game_mech.ConverttoString(big_board,turn," ")
    template_file = "original.html"
    logging.info(template_values)
    path = os.path.join(os.path.dirname(__file__), template_file)
    self.response.out.write(template.render(path, template_values))
  def post(self):
    if self.request.get('block'):
      a = self.request.get('StringMatrix')
      big_board,turn,win = game_mech.ConverttoMatrix(self.request.get('StringMatrix'))
      logging.info(big_board)
      location = self.request.get('block')
      col = int(location[1])-1
      row = int(location[3])-1
      logging.info(win)
      if win != "Win":
        big_board, turn, win,highlight = MoveTTT(col,row,big_board,turn)
      else:
        message,highlight = game_mech.checkifwon(big_board)
      template_values = game_mech.ConverttoDict(big_board)
      logging.info(template_values)
      button_color= HighlightButton(highlight,template_values)
      template_values["button_color"] = button_color
      logging.info(button_color)
      template_values["StringMatrix"] = game_mech.ConverttoString(big_board,turn,win)
      template_values["Winner"] = win
      template_values["Turn"] = str(turn)
      
      template_file = "original.html"
      path = os.path.join(os.path.dirname(__file__), template_file)
      self.response.out.write(template.render(path, template_values))
  def reset(self):
    self.get()

class DevelopersHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), "developers.html")
    self.response.out.write(template.render(path, template_values))

class GameInfoHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), "gameinfo.html")
    self.response.out.write(template.render(path, template_values))

class HelpHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), "help.html")
    self.response.out.write(template.render(path, template_values))

class OriginalGameHandler(webapp.RequestHandler):
  def get(self):
    turn = 1
    big_board = [[0,0,0]]*3
    template_values = game_mech.ConverttoDict(big_board)
    template_values["StringMatrix"] = game_mech.ConverttoString(big_board,turn," ")
    template_file = "original.html"
    logging.info(template_values)
    path = os.path.join(os.path.dirname(__file__), template_file)
    self.response.out.write(template.render(path, template_values))
  def post(self):
    if self.request.get('block'):
      a = self.request.get('StringMatrix')
      big_board,turn,win = game_mech.ConverttoMatrix(self.request.get('StringMatrix'))
      logging.info(big_board)
      location = self.request.get('block')
      col = int(location[1])-1
      row = int(location[3])-1
      logging.info(win)
      if win != "Win":
        big_board, turn, win,highlight = MoveTTT(col,row,big_board,turn)
      else:
        message,highlight = game_mech.checkifwon(big_board)
      template_values = game_mech.ConverttoDict(big_board)
      logging.info(template_values)
      button_color= HighlightButton(highlight,template_values)
      template_values["button_color"] = button_color
      logging.info(button_color)
      template_values["StringMatrix"] = game_mech.ConverttoString(big_board,turn,win)
      template_values["Winner"] = win
      template_values["Turn"] = str(turn)
      
      template_file = "original.html"
      path = os.path.join(os.path.dirname(__file__), template_file)
      self.response.out.write(template.render(path, template_values))
  def reset(self):
    self.get()

def main():
  application = webapp.WSGIApplication([('/original.*', OriginalGameHandler),
                                        ('/crazy.*',CrazyGameHandler),
                                        ('/developers.*', DevelopersHandler),
                                        ('/info.*',GameInfoHandler),
                                        ('/help.*',HelpHandler),
                                        ('/.*',HomeHandler)],
                                     debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
